<?php
class baseMethod extends HController {
	public $initphp_list = array (
			'checkMobile',
			'checkEMail',
			'idcard_checksum18',
			'idcard_15to18',
			'isDate',
			'isDateTime',
			'get_household_type',
			'_password'
	); // Action白名单

	/**
	 * 后台密码加密函数
	 * @param string $password
	 * @return string
	 */
	public function _password($password, $hash) {
		return md5(md5($password) . $hash);
	}
	
	/**
	 * 后台密码加密hash
	 * @return string
	 */
	public function _hash() {
		$function = $this->getLibrary('function'); //共用类
		return $function->get_hash(6);
	}
	
	
	/**
	 * 检测是否是手机号
	 *
	 * @param unknown $str
	 * @return boolean
	 */
	public function checkMobile($str) {
		$pattern = "/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|7|8|9)\d{9}$/"; // 手机号码
		if (preg_match ( $pattern, $str )) {
			Return true;
		} else {
			Return false;
		}
	}

	/**
	 * 检测是否是email
	 *
	 * @param unknown $str
	 * @return boolean
	 */
	public function checkEMail($str) {
		$pattern = "/^[0-9a-zA-Z]+@([0-9a-z]+.)+[a-z]{2,4}$/"; // 电子邮件
		if (preg_match ( $pattern, $str )) {
			Return true;
		} else {
			Return false;
		}
	}
	function idcard_verify_number($idcard_base) {
		if (strlen ( $idcard_base ) != 17) {
			return false;
		}
		// 加权因子
		$factor = array (
		7,
		9,
		10,
		5,
		8,
		4,
		2,
		1,
		6,
		3,
		7,
		9,
		10,
		5,
		8,
		4,
		2
		);

		// 校验码对应值
		$verify_number_list = array (
				'1',
				'0',
				'X',
				'9',
				'8',
				'7',
				'6',
				'5',
				'4',
				'3',
				'2' 
				);

				$checksum = 0;
				for($i = 0; $i < strlen ( $idcard_base ); $i ++) {
					$checksum += substr ( $idcard_base, $i, 1 ) * $factor [$i];
				}

				$mod = strtoupper ( $checksum % 11 );
				$verify_number = $verify_number_list [$mod];

				return $verify_number;
	}

	// 将15位身份证升级到18位
	function idcard_15to18($idcard) {
		if (strlen ( $idcard ) != 15) {
			return false;
		} else {
			// 如果身份证顺序码是996 997 998 999，这些是为百岁以上老人的特殊编码
			if (array_search ( substr ( $idcard, 12, 3 ), array (
					'996',
					'997',
					'998',
					'999' 
					) ) !== false) {
						$idcard = substr ( $idcard, 0, 6 ) . '18' . substr ( $idcard, 6, 9 );
					} else {
						$idcard = substr ( $idcard, 0, 6 ) . '19' . substr ( $idcard, 6, 9 );
					}
		}

		$idcard = $idcard . $this->idcard_verify_number ( $idcard );

		return $idcard;
	}

	// 18位身份证校验码有效性检查
	public function idcard_checksum18($idcard) {
		if (strlen ( $idcard ) != 18) {
			return false;
		}

		$aCity = array (
		11 => "北京",
		12 => "天津",
		13 => "河北",
		14 => "山西",
		15 => "内蒙古",
		21 => "辽宁",
		22 => "吉林",
		23 => "黑龙江",
		31 => "上海",
		32 => "江苏",
		33 => "浙江",
		34 => "安徽",
		35 => "福建",
		36 => "江西",
		37 => "山东",
		41 => "河南",
		42 => "湖北",
		43 => "湖南",
		44 => "广东",
		45 => "广西",
		46 => "海南",
		50 => "重庆",
		51 => "四川",
		52 => "贵州",
		53 => "云南",
		54 => "西藏",
		61 => "陕西",
		62 => "甘肃",
		63 => "青海",
		64 => "宁夏",
		65 => "新疆",
		71 => "台湾",
		81 => "香港",
		82 => "澳门",
		91 => "国外"
		);
		// 非法地区
		if (! array_key_exists ( substr ( $idcard, 0, 2 ), $aCity )) {
			return false;
		}
		// 验证生日
		if (! checkdate ( substr ( $idcard, 10, 2 ), substr ( $idcard, 12, 2 ), substr ( $idcard, 6, 4 ) )) {
			return false;
		}
		$idcard_base = substr ( $idcard, 0, 17 );
		if ($this->idcard_verify_number ( $idcard_base ) != strtoupper ( substr ( $idcard, 17, 1 ) )) {
			return false;
		} else {
			return true;
		}
	}

	//检测日期
	function isDate($date, $format = "Y-m-d") {
		$str_arr = explode("-", $date);
		if (empty($str_arr)) {
			return false;
		}

		foreach($str_arr as $val) {
			if (strlen($val) < 2) {
				$val = "0" . $val;
			}
			$new_date[] = $val;
		}

		$str = implode("-", $new_date);
		$unix_time = strtotime($str);
		$check_date = date($format, $unix_time);
		if ($check_date == $str)
		return true;
		else
		return false;
	}

	//检测日期时间
	function isDateTime($time, $format = "Y-m-d H:i:s") {
		$str_arr = explode(" ", $time);
		if (empty($str_arr)) {
			return false;
		}

		if (!$this->isDate($str_arr[0])){
			return false;
		}

		$str_arr2 = explode(":", $str_arr[1]);

		if (empty($str_arr2)) {
			return false;
		}

		if (count($str_arr2) == 3){
			return true;
		}else{
			return false;
		}
	}

	//产生随机字符串
	function rand_str($len) {
		$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
		$string = '';
		for(;$len >= 1;$len--) {
			$position = rand() % strlen($chars);
			$string .= substr($chars, $position, 1);
		}
		return $string;
	}


	
	/**
	 * 生成订单单号 
	 */
	function get_order_code() {
		$function = $this->getLibrary('function');
		$order_code = '';
		do{
			$order_code = $function->trade_no();
			$cnt = InitPHP::getService ( "ordersmanage" )->getOrdersManageCon(array('order_code'=>$order_code));
		}while ($cnt >0 ); 

		return $order_code;
	}
	

	/**
	 * 今晚方式
	 * @param unknown $n
	 * @param unknown $x
	 * @return number
	 */
	public function process_number($n,$x) {
		$f_x = $x;
		switch($n){
			case 1://四舍五入到元
				$val=round($f_x);
				break;
			case 2://四舍五入到角
				$val=round($f_x*10)/10;
				break;
			case 3://四舍五入到分
				$val=round($f_x*100)/100;
				break;
			case 4://见分进角
				$val=ceil($f_x*10)/10;
				break;
			case 5://见角进元
				$val=ceil($f_x);
				break;
			case 6://取整到元
				$val=intval($f_x);
				break;
			case 7://取整到角
				$val=intval($f_x*10)/10;
				break;
			case 8://取整到分
				$val=intval($f_x*100)/100;
				break;
			case 9://四舍五入保留五位小数
				$val=round($f_x*100000)/100000;
				break;
		}

		return $val;
	}

}